#include<iostream>
#include<vector>
using namespace std;
vector<vector<string>> result;
vector<string> path;
void backtracking(int n,int row,vector<bool> column,vector<bool> right,vector<bool> left)
{
    if(row==n)
    {
        result.push_back(path);
        return;
    }
    string s(n,'.');
    for(int i=0;i<n;i++)
    {
        //判断是否能够放皇后
        if(column[i]||right[i-row+n]||left[i+row])
            continue;
        //能放皇后
        s[i] = 'Q';
        path.push_back(s);
        column[i] = true;
        right[i-row+n] = true;
        left[i+row] = true;
        backtracking(n,row+1,column,right,left);
        //回溯
        s[i] = '.';
        path.pop_back();
        column[i] = false;
        right[i-row+n] = false;
        left[i+row] = false;
    }
}
vector<vector<string>> solveNQueens(int n) {
    vector<bool> column(n,false);
    vector<bool> right(2*n,false);
    vector<bool> left(2*n,false);
    backtracking(n,0,column,right,left);
    return result;
}
int main()
{
    cout<<"enter n:"<<endl;
    int n;
    cin>>n;
    result = solveNQueens(n);
    for(int i=0;i<result.size();i++)
    {
        for(int j=0;j<result[i].size();j++)
        {
            cout<<result[i][j]<<endl;
        }
        cout<<endl;
    }
    return 0;
}